package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class TimeLoginLog extends BaseRepair {

    public TimeLoginLog(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void goLogin(String startTime,String endTime){
        List<Map<String, String>> listMap = new ArrayList<>();
        listMap = getLogLogin(startTime,endTime);
        listMap = getTimeVipExp(listMap,endTime);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLogLogin(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId " +
                " from t_l_login " +
                " where `time` >= ? AND `time` <= ?" +
                " GROUP BY userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getTimeVipExp(List<Map<String,String>> idataList,String beforMaxTime) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_l_vip_exp WHERE userId = ? AND `time` < ? " +
                "ORDER BY time DESC LIMIT 1";
        try {
            for(Map<String,String> data : idataList){
                call = getGameServer().getLogConnection().prepareCall(sql);
                call.setString(1,data.get("userId"));
                call.setString(2,beforMaxTime);
                rs = call.executeQuery();
                if(rs.next()){
                    data.put("vipExp",checkNullToEmpty(rs.getString("nowValue")));
                }else{
                    data.put("vipExp","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

}
